package com.city.parking;

import com.city.parking.util.Utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ParkingPopup {
	public interface OnParkingClickListener {
		public void onParkingClick(ParkingItem park);
		public void onParkingDescClick(ParkingItem park);
	}

	public View mPopupView = null;
	private TextView mNameTV, mModeTV, mIdleTV;
	private OnParkingClickListener mClickListener = null;
	private ParkingItem park;

	public ParkingPopup(Context ctx) {
		LayoutInflater li = LayoutInflater.from(ctx);
		mPopupView = li.inflate(R.layout.parking_popup, null);

		mNameTV = (TextView) mPopupView.findViewById(R.id.parking_name);
		mModeTV = (TextView) mPopupView.findViewById(R.id.parking_mode);
		mIdleTV = (TextView) mPopupView.findViewById(R.id.parking_idle);
		ImageButton btn = (ImageButton) mPopupView.findViewById(R.id.go_btn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (mClickListener != null)
					mClickListener.onParkingClick(park);
			}
		});

		LinearLayout ll = (LinearLayout) mPopupView
				.findViewById(R.id.parking_desc_ll);
		ll.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (mClickListener != null)
					mClickListener.onParkingDescClick(park);
			}
		});
	}

	public void setOnParkingClickListener(OnParkingClickListener listener) {
		mClickListener = listener;
	}

	public View getView() {
		return mPopupView;
	}
	
	public String getDistance(int distance) {
		if (distance < 10000)
			return distance+"m";
		else
			return distance/1000+"km";
	}
	
	public void bindPark(ParkingItem p, int distance) {
		park = p;

		mNameTV.setText(p.name);
		mModeTV.setText("距我：" + getDistance(distance));
		if (p.portLeftCount < 0)
			mIdleTV.setText("空位：未知");
		else
			mIdleTV.setText("空位：" + p.portLeftCount);
		Utils.log("bind parking:" + p.name);
	}
}
